package com.sharemarking.wa.platform.service;

import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.mapper.MemberMapper;
import com.sharemarking.wa.platform.mapper.StatisticsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dxy
 */
@Service
public class StatisticsService {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private StatisticsMapper statisticsMapper;

    public ResponseParams<?> getInfo() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        Integer role = memberMapper.getRoleByMemberId(memberId);

        String year = request.getParameter("year");
        String month = request.getParameter("month");

        if (StringUtils.isEmpty(year)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("year"));
        }
        if (StringUtils.isEmpty(month)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("month"));
        }

        if (-1 > Integer.valueOf(month) || Integer.valueOf(month) > 12) {
            throw RequestException.create(ResponseParams.isDataError("month参数错误"));
        }
        Map<String, Object> params = new HashMap<>();
        params.put("year", year);
        params.put("month", month);
        params.put("memberId", memberId);
        params.put("role", role);


        Map<String, Object> result = new HashMap<>();
        if(Integer.valueOf(month)==-1){
            result.put("publishWasteCount", statisticsMapper.getPublishWasteStatisticsAll(params));
            result.put("publishOrderCount", statisticsMapper.getPublishOrderStatisticsAll(params));
        }else{
            result.put("publishWasteCount", statisticsMapper.getPublishWasteStatistics(params));
            result.put("publishOrderCount", statisticsMapper.getPublishOrderStatistics(params));
        }

        return ResponseParams.ok(result);
    }
}
